/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.javanile.motorino;

import org.javanile.chess.stone.*;
/**
 *
 * @author cicciodarkast
 */
public class Engine {

    private Brain   brain;
    private Console console;

    public Engine(String[] args) {
	console = new Console();
	brain	= new Brain(console);
	brain.setEvaluationFunction(
	    new EvaluationFunction() {
		
	    }
	);
	brain.lauch();
    }

    public void launch() {
        console.put("Motorino Chess Engine | by Francesco Bianco (info.francescobianco@gmail.com)");

	Uci cmd;
	do {
	    cmd = new Uci(console.get());

	    switch(cmd.action) {
		case Uci.UCI:
		    console.put(Uci.ID_NAME	+ "Motorino Chess Engine");
		    console.put(Uci.ID_AUTHOR	+ "Francesco Bianco (info.francescobianco@gmail.com)");
		    console.put(Uci.UCIOK);
		    break;

		case Uci.ISREADY:
		    console.put(Uci.READYOK);
		    break;

		case Uci.POSITION_FEN:
		    brain.newgame(cmd.load);
		    break;
		
		case Uci.POSITION_STARTPOS:
		    brain.newgame();
		    if (cmd.moves.length>0) {
			for(int i=0;i<cmd.moves.length;i++) {
			    brain.eat(cmd.moves[i]);
			}
		    }
		    break;

		case Uci.GO:
		    brain.ponder();
		    break;

		case Uci.STOP:
		    brain.halt();
		    console.put(Uci.BESTMOVE + brain.bestmove());
		    break;

		case Uci.QUIT:
		    brain.quit();
		    console.put("wella!");
		    break;

		case Uci.DEBUG_ON:
		    break;

		case Uci.DEBUG_OFF:
		    break;

		case Uci._DEBUG:
		    brain._debug(cmd.load);
		    break;
	    }
        } while(cmd.prosecute());
    }
}
